package top.linyuxb.lycloud.config.starter.config;

import cn.hutool.core.util.StrUtil;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.boot.env.OriginTrackedMapPropertySource;
import org.springframework.boot.origin.OriginTrackedValue;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.PropertySource;
import top.linyuxb.lycloud.common.common.LyConfigJdbcService;
import top.linyuxb.lycloud.config.starter.common.LyConfigService;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * Created by Intellij IDEA.
 *
 * @Author: linyuxb
 * @Date: 2024/7/5 15:15
 * @Desc: https://blog.csdn.net/weixin_42594143/article/details/133312132
 */
public class LyCustomPropertiesEnvironmentPostProcessor implements EnvironmentPostProcessor {

    @Override
    public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
        PropertySource<?> thisSource = null;
        for (PropertySource<?> propertySource : environment.getPropertySources()) {
            if (propertySource instanceof OriginTrackedMapPropertySource originTrackedMapPropertySource) {
                Map<String, Object> source = originTrackedMapPropertySource.getSource();
                Object object = source.get("ly-config.open");
                if (
                        Objects.nonNull(object) &&
                                object instanceof OriginTrackedValue lyConfigOpen &&
                                Objects.nonNull(lyConfigOpen.getValue()) &&
                                lyConfigOpen.getValue() instanceof Boolean open &&
                                open
                ) {
                    thisSource = new OriginTrackedMapPropertySource(
                            propertySource.getName(), this.doLyCloudConfigInit(source)
                    );
                }
            }
        }
        // 替换原属性
        if (Objects.nonNull(thisSource)) {
            environment.getPropertySources().replace(thisSource.getName(), thisSource);
        }
    }

    /**
     * 初始化配置中心环境
     *
     * @param source
     */
    private Map<String, Object> doLyCloudConfigInit(Map<String, Object> source) {
        Map<String, Object> map = new HashMap<>(source);
        Object driverObject = map.get("ly-config.driver");
        Object urlObject = map.get("ly-config.url");
        Object userNameObject = map.get("ly-config.username");
        Object passwordObject = map.get("ly-config.password");
        String driver = StrUtil.EMPTY;
        if (Objects.nonNull(driverObject) && driverObject instanceof OriginTrackedValue driverValue) {
            driver = driverValue.getValue().toString();
        }
        if (Objects.nonNull(urlObject) &&
                urlObject instanceof OriginTrackedValue urlValue &&
                Objects.nonNull(userNameObject) &&
                userNameObject instanceof OriginTrackedValue userNameValue &&
                Objects.nonNull(passwordObject) &&
                passwordObject instanceof OriginTrackedValue passwordValue
        ) {
            LyConfigJdbcService.initDataSource(driver, urlValue.getValue().toString(),
                    userNameValue.getValue().toString(), passwordValue.getValue().toString());
            LyConfigService.initValue(map);
        }
        return Collections.unmodifiableMap(map);
    }

}